package com.carleasoft.mps.forum.business.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.forum.business.answer.bean.ForumUserPermission;
import com.carleasoft.mps.forum.business.answer.bean.po.ForumUserPo;
import com.carleasoft.mps.forum.business.answer.mapper.ForumUserPermissionMapper;
import com.carleasoft.mps.forum.business.answer.service.ForumUserPermissionService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zang
 * @since 2023-05-09
 */
@Service
public class ForumUserPermissionServiceImpl extends ServiceImpl<ForumUserPermissionMapper, ForumUserPermission> implements ForumUserPermissionService {

    @Override
    public Page<ForumUserPermission> pageForumUser(Page page, ForumUserPo forumUserPo) {
        Page<ForumUserPermission> res = this.getBaseMapper().pageForumUser(page,forumUserPo);
        return res;
    }

    @Override
    public void updateForumStatus(ForumUserPermission forumUserPermission) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();

        List<ForumUserPermission> userPermissionList =
                this.list(new QueryWrapper<ForumUserPermission>().eq("USER_CODE", forumUserPermission.getUserCode()));
        if(userPermissionList.size()>0){
            // 已存在用户信息，直接修改用户的权限
            this.update(new UpdateWrapper<ForumUserPermission>()
                    .set("IS_SUBMIT",forumUserPermission.getIsSubmit())
                    .set("IS_REVIEW",forumUserPermission.getIsReview())
                    .set("UPDATE_PERSON_CODE",user.getUserCode())
                    .set("UPDATE_PERSON_NAME",user.getUserName())
                    .set("UPDATE_TIME",new Date())
                    .eq("USER_CODE",userPermissionList.get(0).getUserCode())
            );
        } else {
            // 不存在用户信息，直接添加用户信息
            forumUserPermission.setCreatePersonCode(user.getUserCode());
            forumUserPermission.setCreatePersonName(user.getUserName());
            forumUserPermission.setCreateTime(new Date());
            this.save(forumUserPermission);
        }

    }
    @Override
    public void updateForumInfo(ForumUserPermission forumUserPermission) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();

        List<ForumUserPermission> userPermissionList =
                this.list(new QueryWrapper<ForumUserPermission>().eq("USER_CODE", forumUserPermission.getUserCode()));
        if(userPermissionList.size()>0){
            // 已存在用户信息，直接修改用户的权限
            this.update(new UpdateWrapper<ForumUserPermission>()
                    .set("NICK_NAME",forumUserPermission.getNickName())
                    .set("PIC_IMG",forumUserPermission.getPicImg())
                    .set("UPDATE_PERSON_CODE",user.getUserCode())
                    .set("UPDATE_PERSON_NAME",user.getUserName())
                    .set("UPDATE_TIME",new Date())
                    .eq("USER_CODE",userPermissionList.get(0).getUserCode())
            );
        } else {
            // 不存在用户信息，直接添加用户信息
            forumUserPermission.setCreatePersonCode(user.getUserCode());
            forumUserPermission.setCreatePersonName(user.getUserName());
            forumUserPermission.setCreateTime(new Date());
            this.save(forumUserPermission);
        }

    }
}
